package recursion;

/**
 * @program: play-structure
 * 移除链表元素,删除链表中等于给定值 val 的所有节点，简化一些代码
 * @author: baichen
 **/
public class RemoveLinkedListElement2 {
    public ListNode removeElements(ListNode head, int val) {
        while (head != null && head.val == val)
            head = head.next;

        if (head == null)
            return head;

        ListNode prev = head;
        while (prev.next != null) {
            if (prev.next.val == val)
                prev.next = prev.next.next;
            else
                prev = prev.next;
        }

        return head;
    }
}
